<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 设置div并给div命名和设置样式 -->
    <div
      id="divObj"
      style="width: 300px; height: 300px; background-color: pink"
    ></div>

    <script>
      // 声明div对象
      var divObj = document.querySelector("#divObj");
      divObj.onmousemove = function (e) {
        var e = e || window.event;
        console.log(e.offsetX);
        console.log(e.offsetY);
        this.innerHTML = e.offsetX + "," + e.offsetY;
      };
    </script>
  </body>
</html>
